{% extends "layout.html" %}
{% block css %}
<link rel="stylesheet" type="text/css" href="{{url('/assets/css/plugins/webuploader/webuploader.css')}}">
<link rel="stylesheet" type="text/css" href="{{url('/assets/css/plugins/webuploader/webuploader_excel.css')}}">

 <style>.form-horizontal select.m-b{padding-top: 3px}</style>
 
{% endblock %}
{% block content %}
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>成绩管理 <small> 录入成绩</small></h5>
                        <div class="ibox-tools">
                           
                        </div>
                    </div>
                    <div class="col-xs-5 col-md-5">
                        <a href="{{url('/student/index.html')}}"><button type="button" class="btn btn-w-m btn-success"><i class="fa fa-mail-reply"></i> 返回列表</button></a>
                        <a onClick="javascript:window.location.reload();"><button type="button" class="btn btn-w-m btn-info"><i class="fa fa-undo"></i> 刷新页面</button></a>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="fractionForm">
                            <input id="student_id" name="student_id" class="form-control" type="hidden" value="{{student.id}}">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">学生学号：</label>
                                <div class="col-sm-7">
                                    <input id="number" name="number" disabled="disabled" value="{{student.number}}" class="form-control" type="text">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">学生姓名：</label>
                                <div class="col-sm-7">
                                    <input id="username"  name="username" disabled="disabled" value="{{student.username}}" class="form-control" type="text">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">上课学期：</label>
                                <div class="col-sm-7">
                                    <select id="semester_id" class="form-control m-b" name="semester_id" data-value="{{student.semester_id}}">
                                         <option value="">全部</option>
                                         {% if semesters|length > 0 %}
                                             {% for semester in semesters %}
                                                 <option value="{{ semester.id|e }}">{{ semester.name|e }}</option>
                                             {% endfor %}
                                         {% endif %}
                                    </select>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                {% if subjects|length > 0 %}
                                    <label class="col-sm-3 control-label">选择课程：</label>
                                    <div class="col-sm-7">
                                       <select id="subject_id" class="form-control m-b" name="subject_id">
                                            <option value="">请选择课程名称</option>
                                                {% for subject in subjects %}
                                                    <option value="{{ subject.id|e }}">[{{ subject.teacher_id|e }}]-{{ subject.subject_id|e }}</option>
                                                {% endfor %}
                                        </select>
                                    </div>
                                {% else %}
                                    <label class="col-sm-3 control-label">在线选课：</label>
                                    <div class="col-sm-7">
                                       <a type="button" href="{{elective}}" class="btn btn-w-m btn-info">在线选课</a>
                                       <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>该学生没有选课，无法录入成绩</span>
                                    </div>
                                {% endif %}
                            </div>
                            
                            <div class="form-group">
                                <label class="col-sm-3 control-label">学生成绩：</label>
                                <div class="col-sm-7">
                                    <input id="score" name="score" class="form-control" type="text">
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-sm-3 control-label">教师点评：</label>
                                <div class="col-sm-7">
                                    <textarea row="2" id="comment" name="comment" class="form-control" ></textarea>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <div class=" col-sm-3 col-sm-offset-3">
                                    <button class="btn btn-default" type="reset">重置</button>
                                    {% if subjects|length > 0 %}
                                        <button class="btn btn-primary" type="submit">提交</button>
                                    {% else %}
                                        <button class="btn btn-primary" disabled="disabled" type="button">提交</button>
                                    {% endif %}
                            </div>
                            
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
{% endblock %}
{% block script %}
<!-- jQuery Validation plugin javascript-->
<script src="{{url('/assets/js/plugins/validate/jquery.validate.min.js')}}"></script>
<script src="{{url('/assets/js/plugins/validate/messages_zh.min.js')}}"></script>

<script src="{{url('/assets/js/plugins/layer/layer.min.js')}}"></script>


<script>
$(function($) {

       
        var icon = "<i class='fa fa-times-circle'></i> ";
        //录入分数是否正确  
        jQuery.validator.addMethod("isScore", function(value, element) {  
            var regScore = /^[0-9]{1,3}$/;  
            var score = Number(value);
            var scoreNum;
            if((0 <= score) && (score < 101)){
                scoreNum = true;
            }
            return this.optional(element) || ( scoreNum == true && regScore.test( value ) );    
        }, "请正确填写学生分数");
        
        // 表单验证
        var validate = $("#fractionForm").validate({
            debug: false, //调试模式取消submit的默认提交功能 
            rules: {
                subject_id: {
                    required: true
                },
                score:{
                    required: true,
                    isScore:true
                },
                comment: {
                    required: true,
                    maxlength:255
                }
            },
            messages: {
                subject_id: {
                    required: icon + "请选择学生科目"
                },
                score: {
                    required: icon + "请输入学生成绩"
                },
                comment: {
                    required: icon + "请简单点评学生",
                    maxlength:icon + "输入长度最多是 255 的字符串"
                }
            },
            submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form   
                var datas = $(form).serialize();
                $.ajax({
                    type: "POST",
                    url: "{{ url('/fraction/adddata.html') }}",
                    data: datas,
                    dataType: "json",
                    success: function(dataJson){
                        if(dataJson.valid === 0){
                           var message = dataJson.message;
                           $.each(message,function(i,v){  // 后端验证提示
                               var error ='<span class="help-block m-b-none"><i class="fa fa-times-circle"></i> '+v+'</span>';
                               $('#'+i).after(error);
                           });
                       }else{
                           text = 'error';
                           if(dataJson.status == 200800){
                              text = 'success';
                           }
                           $.AngelaAutoTips({
                               msg : dataJson.message, //显示的消息
                               type : text		   //提示类型（1、success 2、error 3、warning）
                           });
                       }
                    },
                    error:function(){
                        $.AngelaAutoTips({
                            msg : ' 很抱歉！录入失败，请核实信息是否正确', //显示的消息
                            type : 'error'		   //提示类型（1、success 2、error 3、warning）
                        });
                    }
                });
            }
        });  // 表单验证  
    });
    
    var selectEdit = function(){
        var selectDOM = [
            'semester_id'
        ];
        $.each( selectDOM , function(index,value){
            $("#"+value).find('option').each(function(i){
                var va = $(this).val();
                var dva = $("#"+value).attr('data-value');
                if(Number(va) == Number(dva)){
                    $(this).prop("selected", true);
                }
            });
        });
    };
    selectEdit(); // 执行
</script>
{% endblock %}